package cn.cnnic.uploadkit.example;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.cnnic.uploadkit.bean.FormFile;
import static cn.cnnic.uploadkit.MultipartFormdataUtil.*;

@SuppressWarnings("serial")
public class UploadTest extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Map<String, Object> parameters = resolveRequestData(request.getInputStream(), request.getContentType());

		for (Iterator<String> it = parameters.keySet().iterator(); it.hasNext();) {
			String key = it.next();
			if (key.equals("tfile")) {
				response.getOutputStream().write(((FormFile) parameters.get(key)).getData());
			}else{
				System.err.println(key + ":" + parameters.get(key));
			}
		}
	}

}
